﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 坦克大战.Properties;

namespace 坦克大战
{
    enum Tag
    {
        MyTank,
        EnemyTnak
    }

    internal class Bullet : Movingthing
    {
        public Tag Tag { get; set; }

        public bool IsDestroy { get; set; }
        public Bullet(int x, int y, int speed, Dirction dir,Tag tag)
        {
            IsDestroy = false;
            this.X = x;
            this.Y = y;
            this.Speed = speed;
            this.BitmapLeft = Resources.BulletLeft;
            this.BitmapUp = Resources.BulletUp;
            this.BitmapRight = Resources.BulletRight;
            this.BitmapDown = Resources.BulletDown;
            this.Dir = dir;
            this.X -= Width / 2;
            this.Y -= Height / 2;
            this.Tag = tag;


        }

        public override void DrawSelf()
        {
            base.DrawSelf();
        }
        public override void Update()
        {
            MoveCheck();//移动检查
            Move();

            base.Update();
        }

        private void MoveCheck()
        {

            #region 检查有没有超过窗体边界
            if (Dir == Dirction.Up)
            {
                if (Y+Height/2+3 < 0)
                {
                    IsDestroy=true ; return;
                }
            }
            else if (Dir == Dirction.Down)
            {
                if (Y + Height/2-3 > 450)
                {
                    IsDestroy = true; return;
                }
            }
            else if (Dir == Dirction.Left)
            {
                if (X+Width/2-3 < 0)
                {
                    IsDestroy = true; return;
                }
            }
            else if (Dir == Dirction.Right)
            {
                if (X+Width/2+3 > 450)
                {
                    IsDestroy = true; return;
                }
            }
            #endregion


            //检查有没有和其他元素发生碰撞

            Rectangle rect = GetRectangle();

            rect.X = X + Width / 2 - 3;
            rect.Y = Y + Height / 2 - 3;
            rect.Height = 3;
            rect.Width = 3;

            int xExp = this.X + Width / 2;
            int yExp= this.Y + Height / 2;

            NotMoving wall = null;
            if ((wall=GameObjectManager.IsCollidedWall(rect)) != null)
            {
                IsDestroy = true; 
                GameObjectManager.DestoryWall(wall);
                GameObjectManager.CreateExp(xExp, yExp);
                SoundManager.PlayBlast();
                return;
            }
            if (GameObjectManager.IsCollidedSteel(rect) != null)
            {
                GameObjectManager.CreateExp(xExp, yExp);
                IsDestroy = true; return;
            }
            if(Tag==Tag.MyTank)
            {
                EnemtTank tank =null;
                if((tank=GameObjectManager.IsCollidedEnemyTank(rect))!=null)
                {
                    IsDestroy=true;
                    GameObjectManager.DestoryTank(tank);
                    GameObjectManager.CreateExp(xExp, yExp);
                    SoundManager.PlayHit();
                    return;
                }
            }
            else if (Tag==Tag.EnemyTnak)
            {
                MyTank tank = null;
                if ((tank = GameObjectManager.IsCollidedMyTank(rect)) != null)
                {
                    IsDestroy = true;
                    
                    GameObjectManager.CreateExp(xExp, yExp);
                    tank.TakeDamage();
                    SoundManager.PlayBlast();


                    return;
                }
            }


            if (GameObjectManager.IsCollidedBoss(rect))
            {
                SoundManager.PlayBlast();
                GameFrameWork.ChangeToGameOver(); return;
            }
        }

        private void Move()
        {


            switch (Dir)
            {
                case Dirction.Up:
                    Y -= Speed;
                    break;
                case Dirction.Down:
                    Y += Speed;
                    break;
                case Dirction.Left:
                    X -= Speed;
                    break;
                case Dirction.Right:
                    X += Speed;
                    break;
            }
        }


    }





}
